$(function() {
    layui.use(['laypage', 'layer','jquery','table'],

        function() {
            $ = layui.jquery;
            var laypage = layui.laypage,
                layer = layui.layer,
                table = layui.table;

            var parameter =  {
                page : 1,
                limit : 10,
                req : {
                }
            }

            var rendData = function (data) {
                table.render({
                    elem: '#visit-log'
                    ,cols: [[{type: 'checkbox', width: 30,fixed: 'left'},
                        {field: 'logId', title: '日志id', width:60,}
                        ,{field: 'threadId', title: '线程ID', width:250}
                        ,{field: 'ipAddr', title: '访问ip', width:180, sort: true}
                        ,{field: 'url', title: '访问url', width:160, sort: true}
                        ,{field: 'httpMethod', title: '请求方式', width: 180}
                        ,{field: 'visitControllerMethod', title: '控制器', width: 120}
                        ,{field: 'timeCost', title: '访问耗时', width: 80, sort: true}
                        ,{field: 'os', title: '操作系统', width: 80, sort: true}
                        ,{field: 'browser', title: '浏览器', width: 160, sort: true}
                        ,{field: 'userAgent', title: '请求头userAgent', width: 160, sort: true}
                        ,{field: 'createTime', title: '创建时间', width: 160, sort: true}
                    ]],
                    data: data,
                    even: true,
                    limit: data.length,
                    limits: [10, 20, 30, 40, 50],
                    toolbar: true
                });
            };

            var laypageTable = function (total) {
                laypage.render({
                    elem: 'laypage',
                    count: total,
                    limit: parameter.limit,
                    curr: parameter.page,
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            parameter.page = obj.curr;
                            parameter.limit = obj.limit;
                            loadData();
                        }
                    }
                });
            };


            var loadData = function (){
                $.ajax({
                    url:'/visit-log/log-list',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data: JSON.stringify(parameter),
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    contentType: "application/json",
                    success:function(data){
                        if (data.code == '0'){
                            rendData(data.data.records);
                            laypageTable(data.data.total);
                        }else {
                            layer.msg("系统错误！",{icon:2});
                        }
                    }
                });
            }

            loadData();

        })
})